﻿using HZH_Controls.Forms;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace HangzhouChipProject
{
    public partial class Calibration : FrmWithOKCancel1
    {
        double MachineStarting;
        double MachineEnd;
        double PixelStarting;
        double pixelEnd;
        public static double pixelEqu;       // 像素当量，一个像素对于走多少毫米
        public static double XAxilelOffset; // X轴偏移距离
        public static double YAxileOffset;  // Y轴偏移距离
        MainWindows mainWindows;
        public Calibration(MainWindows mainwindow)
        {
            InitializeComponent();
            this.mainWindows = mainwindow;
            this.Title = "标定";
            this.IsShowCloseBtn = true;
            this.BackColor = Color.FromArgb(128, 128, 255);

         
        }
        private void Calibration_Load(object sender, EventArgs e)
        {
            loadDataCalibration();
        }
        protected override void DoEnter()
        {
            SaveDataCalibration();
            mainWindows.PrintfLog("数据保存成功");
        }
        private void SaveDataCalibration()
        {
            Properties.Calibration.Default.XaxileOffset = this.textBoxEx_XAxileOffset.Text;
            Properties.Calibration.Default.YaxileOffset = this.textBoxEx_YAxileOffset.Text;
            Properties.Calibration.Default.PixelEquival = this.textBoxEx_PixelEquivalent.Text;
            Properties.Calibration.Default.Save();
        }
        private void loadDataCalibration()
        {
            try
            {
                this.textBoxEx_XAxileOffset.Text = Properties.Calibration.Default.XaxileOffset;
                this.textBoxEx_YAxileOffset.Text = Properties.Calibration.Default.YaxileOffset;
                this.textBoxEx_PixelEquivalent.Text = Properties.Calibration.Default.PixelEquival;
                pixelEqu = Convert.ToDouble(Properties.Calibration.Default.PixelEquival);
                XAxilelOffset = Convert.ToDouble(Properties.Calibration.Default.XaxileOffset);
                YAxileOffset = Convert.ToDouble(Properties.Calibration.Default.YaxileOffset);
            }
            catch (Exception ex)
            {

                FrmTips.ShowTipsWarning(this,"载入标定参数数据时："+ ex.Message.ToString());
            }
           
        }
        private void ucBtnImg_CalculateEquivalent_BtnClick(object sender, EventArgs e)
        {
            FD();
            pixelEqu= ComputerData(MachineStarting,  MachineEnd,  PixelStarting,  pixelEnd);
            if (pixelEqu==0)
            { 
                FrmTips.ShowTipsWarning(this,"请检查输入的数值");
                return;
            }
            this.textBoxEx_PixelEquivalent.Text = null;
            this.textBoxEx_PixelEquivalent.Text = pixelEqu.ToString("f5");
            mainWindows.PrintfLog(string.Format("计算出像素当量是：{0}",pixelEqu));
        }
        private double ComputerData(double MachineStarting, double MachineEnd, double PixelStarting, double pixelEnd)
        {
            if (MachineStarting !=0 && MachineEnd != 0 && PixelStarting != 0 && pixelEnd !=0)
            {
                double pixleequ = (MachineEnd - MachineStarting)/(pixelEnd- PixelStarting);
                return pixleequ;
            }
            return 0;
        }
        /// <summary>
        /// 获取界面测试的参数值
        /// </summary>
        private void FD()
        {
            try
            {
                MachineStarting = Convert.ToDouble(this.textBoxEx_MachineStarting.Text);
                MachineEnd = Convert.ToDouble(this.textBoxEx_MachineEnd.Text);
                PixelStarting = Convert.ToDouble(this.textBoxEx_PixelStarting.Text);
                pixelEnd = Convert.ToDouble(this.textBoxEx_PixelEnd.Text);
                XAxilelOffset = Convert.ToDouble(this.textBoxEx_XAxileOffset.Text);
                YAxileOffset = Convert.ToDouble(this.textBoxEx_YAxileOffset.Text);
            }
            catch (Exception ex)
            {

                FrmTips.ShowTipsError(this,ex.Message.ToString());
            }
         
        }

        private void textBoxEx_XAxileOffset_MouseLeave(object sender, EventArgs e)
        {
            if (this.textBoxEx_XAxileOffset.Text.Length==0)
            {
                this.textBoxEx_XAxileOffset.Text = 0.ToString();
            }
        }

        private void textBoxEx_YAxileOffset_MouseLeave(object sender, EventArgs e)
        {
            if (this.textBoxEx_YAxileOffset.Text.Length == 0)
            {
                this.textBoxEx_YAxileOffset.Text = 0.ToString();
            }
        }


    }
}
